﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using CRM.Entity;
using CRM.BLL;
using System.Text;

namespace CRM.Web.FaqManage
{
    public partial class FaqDirectManageLeftTree : Common.PageBase
    {
        private UserLoginInfo _loginInfo;

        protected override void OnInit(EventArgs e)
        {
            base.VerificationRight = false;
            base.OnInit(e);
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            UserLoginInfo.CheckLoginInfo();
            _loginInfo = UserLoginInfo.GetLoginInfo();
            if (!IsPostBack)
            {
                TreeBinding();
            }
        }

        private void TreeBinding()
        {
            string urlpath = GetServerPath();
            StringBuilder strb = new StringBuilder();
            strb.Append("d = new dTree('d');");
            strb.Append("d.add(0,-1,'FAQ目录','FaqDirectManageRightContent.aspx?CATID=0&CATNAME=FAQ目录','', 'frameFaqMain');");

            if (UserLoginInfo.IsAdministrator())
            {
                T_FAQ_TREE_BLL bll = new T_FAQ_TREE_BLL();
                List<T_FAQ_TREE_Entity> list = bll.GetList(" AND ISACTIVE = 1", " CATID ASC,ORDERNUM ASC ");
                RecursiveTreeBinding(list, "0", strb);
            }
            else
            {
                T_FAQ_TREE_BLL bll = new T_FAQ_TREE_BLL();
                List<T_FAQ_TREE_Entity> list = bll.GetList(String.Format(" AND ISACTIVE = 1 and BUSINESSDIVISIONCODE = '{0}' ", _loginInfo.UserEntity.BUSINESSDIVISIONCODE), " CATID ASC,ORDERNUM ASC ");
                RecursiveTreeBinding(list, "0", strb);
            }
            strb.Append("document.getElementById('divMainFAQ').innerHTML += d;");
            //strb.Append("d.closeAll();");

            Page.ClientScript.RegisterStartupScript(this.GetType(), "tree", strb.ToString(), true);
        }

        private void RecursiveTreeBinding(List<T_FAQ_TREE_Entity> list, string parentID, StringBuilder strb)
        {
            List<T_FAQ_TREE_Entity> subList = list.FindAll(delegate(T_FAQ_TREE_Entity en) { if (en.FID.ToString() == parentID) return true; else return false; });
            foreach (T_FAQ_TREE_Entity entity in subList)
            {
                string url = string.Format("FaqDirectManageRightContent.aspx?CATID={0}&CATNAME={1}", entity.CATID.ToString(),Server.UrlEncode(entity.CATNAME.ToString()));
                strb.Append(String.Format("d.add({0},{1},'{2}','{3}', '{2}', '{4}');", entity.CATID.ToString(), entity.FID.ToString(), entity.CATNAME.ToString(), url, "frameFaqMain"));
                RecursiveTreeBinding(list, entity.CATID.ToString(), strb);
            }
        }

        private void RecursiveTreeBinding(List<V_ROLEMENU_Entity> list, string parentID, StringBuilder strb)
        {
            List<V_ROLEMENU_Entity> subList = list.FindAll(delegate(V_ROLEMENU_Entity en) { if (en.PARENTMENUID.ToString() == parentID) return true; else return false; });
            foreach (V_ROLEMENU_Entity entity in subList)
            {
                string url = entity.PAGEPATH.TrimEnd('/') + "/" + entity.PAGENAME;
                strb.Append(String.Format("d.add({0},{1},'{2}','{3}', '{2}', '{4}');", entity.MENUID.ToString(), entity.PARENTMENUID.ToString(), entity.MENUNAME.ToString(), url, entity.MENUTARGET.Trim()));
                RecursiveTreeBinding(list, entity.MENUID.ToString(), strb);
            }
        }

        private string GetServerPath()
        {
            string url = Request.Url.AbsoluteUri.Replace("TreeList.aspx", "");
            return url;
        }
    }
}
